package com.aiwu

import android.app.Activity
import com.aiwu.EntryActivity
import com.aiwu.library.abs.ui.AbsDownloadStateActivity
import com.aiwu.library.util.IOUtil
import com.aiwu.utils.CitraUtil
import com.aiwu.utils.GameIdUtil
import java.io.File

/**
 * 下载存档
 */
class DownloadStateActivity : AbsDownloadStateActivity() {
    override fun onPermissionGranted() {
        super.onPermissionGranted()
        IOUtil.createOrExistsDir(File(CitraUtil.getStateDir()))
    }

    override fun getStateFilePath(romName: String, gameFilePath: String, id: Int): String {
        val slot = id.toString().padStart(2, '0')
        val gameId = getGameId(romName, gameFilePath)
        return "${CitraUtil.getStateDir()}/${gameId}${StateManager.STATE_VERSION_TAG}.${slot}.cst"
    }

    override fun getStatePicFilePath(romName: String, gameFilePath: String, id: Int): String {
        val gameId = getGameId(romName, gameFilePath)
        return "${CitraUtil.getStateDir()}/${gameId}${StateManager.STATE_VERSION_TAG}_${id}.png"
    }

    /**
     * 防止传递的游戏编号无效
     */
    private fun getGameId(romName: String, gameFilePath: String): String {
        if (GameIdUtil.is3DSGameId(romName)) {
            return romName
        }
        return GameIdUtil.getGameId(gameFilePath) ?: "未找到游戏编号"
    }

    override fun getEntryAct(): Class<out Activity?> {
        return EntryActivity::class.java
    }
}
